package com.itcast.donkey.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itcast.donkey.R;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;


/**
 * Created by lanouhn on 16/9/8.
 */
public class SetUpActivity extends Activity implements View.OnClickListener {
    private ImageView logo;
    private TextView click_login, msg_push_set, clear_cache, check_new_version;

    private Button btn_out_enter;
    private TextView tv_head_icon, tv_link_internet;
    private View viewLine;

    //清楚缓存
    private TextView tv_zero;

    //从SharedPreferences里边获取到是否登陆过，并且登出后，改变isEnter
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private boolean isEnter = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_up);
        sp = getSharedPreferences("config", MODE_PRIVATE);
        editor = sp.edit();
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        logo = (ImageView) findViewById(R.id.logo);
        click_login = (TextView) findViewById(R.id.click_login);
        msg_push_set = (TextView) findViewById(R.id.msg_push_set);
        clear_cache = (TextView) findViewById(R.id.clear_cache);
        check_new_version = (TextView) findViewById(R.id.check_new_version);

        btn_out_enter = (Button) findViewById(R.id.btn_out_enter);
        tv_head_icon = (TextView) findViewById(R.id.click_head_icon);
        tv_link_internet = (TextView) findViewById(R.id.tv_link_internet);


        tv_zero = (TextView) findViewById(R.id.tv_zero);
        viewLine = (View) findViewById(R.id.view_link_below);


    }


    private void initData() {
        isEnter = sp.getBoolean("isEnter", true);

        if (isEnter) {
            //没有登陆过就正常显示
        } else {
            click_login.setVisibility(View.GONE);

            btn_out_enter.setVisibility(View.VISIBLE);
            viewLine.setVisibility(View.VISIBLE);
            tv_head_icon.setVisibility(View.VISIBLE);
            tv_link_internet.setVisibility(View.VISIBLE);
        }

    }

    private void initEvent() {
        logo.setOnClickListener(this);
        click_login.setOnClickListener(this);
        msg_push_set.setOnClickListener(this);
        clear_cache.setOnClickListener(this);
        check_new_version.setOnClickListener(this);

        btn_out_enter.setOnClickListener(this);
        tv_head_icon.setOnClickListener(this);
        tv_link_internet.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logo:
                Intent intent = new Intent(SetUpActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.click_login:
                Intent intent1 = new Intent(SetUpActivity.this, LoginActivity.class);
                startActivity(intent1);
                break;
            case R.id.msg_push_set:
                Intent intent2 = new Intent(SetUpActivity.this, MsgPushSetActivity.class);
                startActivity(intent2);
                break;
            case R.id.clear_cache:
                AlertDialog.Builder builder = new AlertDialog.Builder(SetUpActivity.this);
                builder.setMessage("你确定要清除所有缓存吗?");
                builder.setTitle("蝉游记");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tv_zero.setText("0.0M");
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                break;
            case R.id.check_new_version:
                Toast.makeText(this, "已经是最新版本", Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_out_enter:
                //退出登录
                /**
                 * 分享的弹出框
                 */
                android.support.v7.app.AlertDialog.Builder builder1 = new android.support.v7.app.AlertDialog.Builder(SetUpActivity.this);
                View view1 = View.inflate(SetUpActivity.this, R.layout.dialog_out_enter, null);
                builder1.setView(view1);
                builder1.setCancelable(false);
                final android.support.v7.app.AlertDialog ad = builder1.create();
                TextView btn_cancel = (TextView) view1.findViewById(R.id.btn_cancel);
                TextView btn_certain = (TextView) view1.findViewById(R.id.btn_certain);
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.cancel();
                    }
                });
                btn_certain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.cancel();
                        ShareSDK.initSDK(SetUpActivity.this);
                        Platform qq = ShareSDK.getPlatform(SetUpActivity.this, QQ.NAME);
                        if (qq.isValid()) {
                            qq.removeAccount();
                        }
                        editor.putBoolean("isEnter", true);
                        editor.commit();
                        Intent intent3 = new Intent(SetUpActivity.this, SetUpActivity.class);
                        startActivity(intent3);
                        finish();
                    }
                });
                ad.show();
                break;

            case R.id.click_head_icon:
                //点击头像进行设置
                Toast.makeText(this, "设置头像", Toast.LENGTH_SHORT).show();
                break;

            case R.id.tv_link_internet:
                //点击是否连接网络
                Toast.makeText(this, "连接社交网络", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
